﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Extensions {
    public static class TransformExtensions
    {
        /// <summary>
        /// 获取更近的边
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="camera"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static int CloserEdge(this Transform transform,Camera camera,int width,int height) {
            // 首先计算出在世界坐标下，上下边的坐标
            Vector3 worldPointTop = camera.ScreenToWorldPoint(new Vector3(width / 2f, height, 0));
            Vector3 worldPointBot = camera.ScreenToWorldPoint(new Vector3(width / 2f, 0, 0));

            // 然后计算出这个坐标与上下边的距离
            float deltaTop = Vector2.Distance(worldPointTop, transform.position);
            float deltaBot = Vector2.Distance(worldPointBot, transform.position);

            // 最后返回的是这个坐标跟上下边哪个最接近
            return deltaBot <= deltaTop ? 1 : -1;
        }
    }
}
